home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / 4tune.zip / 4TUNE.C next >
Text File  |  1990-08-10  |  9KB  |  326 lines

  1. /*
  2. *
  3. *                             4tune
  4. *
  5. *
  6. *                                            Luca Favaro
  7. *
  8. */
  9.  
  10. #include <string.h>
  11. #include <dos.h>
  12. #include <stdio.h>
  13. #include <io.h>
  14. #define version "4Tune - Version 2.60 - June 1990 - by Luke"
  15.  
  16.   int p_opt, f_opt, o_opt = 0;
  17.   char name[30];
  18.   FILE *stream;
  19.   unsigned long flength;
  20.   unsigned long offset;
  21.   char uno[] = "$e[s$e[H$e[0;1;37;44m";
  22.   char due[] = "$e[K$e[u$e[0m";
  23.   char full[256];
  24.   char text[81];
  25.   unsigned int psp;
  26.   int size;
  27.   int full_size;
  28.   char far *env;
  29.  
  30. /* --------------------------------------------------------------------*/
  31. /*     main                                   */
  32. /* --------------------------------------------------------------------*/
  33.  
  34. main(int argc, char *argv[])
  35. {
  36.   int num = 0;
  37.  
  38. /*  Set p_opt, f_opt and o_opt accordingly to the options present (1=present) */
  39.  
  40.   while ((++num) < argc)
  41.   {
  42.     if (stricmp (argv[num],"?") == 0)
  43.     {
  44.         disp_usage();
  45.         continue;
  46.     }
  47.     if (stricmp (argv[num],"/p") == 0)
  48.     {
  49.         p_opt = 1;
  50.         continue;
  51.     }
  52.     if (stricmp (argv[num],"/o") == 0)
  53.     {
  54.         o_opt = 1;
  55.         continue;
  56.     }
  57.     f_opt = num;
  58.   }
  59.  
  60.   if (f_opt != 0)               /* is the FILE opt. present?... */
  61.      strcpy(name,argv[f_opt]);     /* ...YES! Let's use the optional name  */
  62.   else
  63.      strcpy(name,"TUNE");          /* ...NO! Let's use "TUNE" filename */
  64.   open_file();
  65.   while (strlen(text) == 0)
  66.   {
  67.     find_random();
  68.     get_string();
  69.   }
  70.   if (o_opt != 0)     printf("\nString lenght:   %d\n\n",strlen(text));
  71.   if (p_opt != 0)
  72.   {
  73.     printf("\n%s",version); /* if P option present display an header .... */
  74.     find_psp();        /* ...then look for PSP... */
  75.     extract_prompt();    /* ...extract the PROMPT variable... */
  76.  
  77.          /* if we run out of environment space, display a message .... */
  78.  
  79.     if  ((43 + 80) >= size)
  80.         reset_pr();            /* ...reset prompt and exit */
  81.  
  82.     write_in_psp();                /* ...and write in it */
  83.   }
  84.   else            /* if no P option present, just display the fortune */
  85.     disp_string();
  86.   fcloseall();            /* ....done */
  87.   exit(1);
  88. }
  89.  
  90. /* --------------------------------------------------------------------*/
  91. /*  this function opens the file                       */
  92. /* --------------------------------------------------------------------*/
  93. open_file()
  94. {
  95.   stream = fopen(name,"r");                          /* open the file */
  96.   if (stream == NULL)                     /* if no FILE found...    */
  97.   {
  98.      printf ("\nCan't open input file: %s\n",name);
  99.      exit(1);
  100.   }
  101.   flength = filelength (fileno(stream)) - 2; /* get length of FILE in bytes */
  102. }
  103.  
  104. /* --------------------------------------------------------------------*/
  105. /*  this function gives a random value between 1 and the file size     */
  106. /* --------------------------------------------------------------------*/
  107. find_random()
  108. {
  109.   unsigned long rand_value1, rand_value2;
  110.   long now;
  111.   int loop;
  112.  
  113. /* Generate random seed */
  114.  
  115.   time(&now);
  116.   loop = abs(* (char *) 0x0440);
  117.   srand(loop + now);
  118.  
  119. /* Get first random number */
  120.  
  121.   loop = abs(* (char *) 0x046C);
  122.   while (loop >= 0)
  123.   {
  124.      loop--;
  125.      rand();
  126.   }
  127.   rand_value1 = ((long) rand() * 10000) / 32767;   /* number between 0 and 9999 */
  128.  
  129.   rand_value1 = rand_value1 * 100;
  130.  
  131. /* Get second random number */
  132.  
  133.   loop = abs(* (char *) 0x046D);
  134.   while (loop >= 0)
  135.   {
  136.      loop--;
  137.      rand();
  138.   }
  139.  
  140. /* rand_value can be a number between 0 and 1,000,000 */
  141.  
  142.   rand_value2 = rand_value1 + (((long) rand() * 100) / 32767);
  143.  
  144. /* define an offset in the file */
  145.  
  146.   offset =  ((float) rand_value2 / 1000000.0) * flength;
  147.  
  148.   if (o_opt == 1)
  149.   {
  150.     printf ("\nRandom value1:   %lu",rand_value1 / 100);
  151.     printf ("\nRandom value2:   %lu",rand_value2 - rand_value1);
  152.       printf ("\nFile lenght:     %lu",flength);
  153.     printf ("\nOffset:          %lu",offset);
  154.   }
  155. }
  156.  
  157. /* --------------------------------------------------------------------*/
  158. /*      This function reads a string from the file.                  */
  159. /* --------------------------------------------------------------------*/
  160. get_string()
  161. {
  162.   char *pointer_f;
  163.   int pointer_t = 0;
  164.  
  165.   fseek(stream, offset, 0);            /* read 162 character starting from the... */
  166.   fread(full, 162, 1, stream);        /* ...specifed offset in the file */
  167.   pointer_f = strchr(full,'\n');        /* Find first <CR> in string. */
  168.   pointer_f++;                          /* move to the next character.... */
  169.   if ((strchr(pointer_f,'\n') - pointer_f) > 80)
  170.   {
  171.     printf ("\nFile contains a string longer than 80 characters/n");
  172.     exit (1);
  173.   }
  174.   while (*pointer_f != '\n')            /* ...copy from full[] to text[].. */
  175.   {                                     /* ...'till next <CR>  */
  176.     text[pointer_t] = *pointer_f;
  177.     pointer_f++;
  178.     pointer_t++;
  179.   }
  180.   text[pointer_t--] = '\0';
  181. }
  182.  
  183. /* --------------------------------------------------------------------*/
  184. /*    This function finds command.com PSP and environment size       */
  185. /* --------------------------------------------------------------------*/
  186. find_psp()
  187. {
  188.   unsigned *tmp;
  189.  
  190.   psp = _psp;
  191.   tmp = MK_FP(psp,22);
  192.  
  193.   while (psp != *tmp)
  194.   {
  195.      psp = *tmp;
  196.   }
  197.   env = MK_FP(*(unsigned far *)MK_FP(psp,44),0);
  198.   size = 16 * *(int far *) MK_FP(FP_SEG(env)-1,3);
  199.   full_size = size;
  200. }
  201.  
  202. /* --------------------------------------------------------------------*/
  203. /*    This function extract and delete the PROMPT value in PSP           */
  204. /* --------------------------------------------------------------------*/
  205. extract_prompt()
  206. {
  207.   char far *pr_pointer;
  208.   char far *env_tmp;
  209.   char *x;
  210.  
  211. /* in the PSP segment each variable is separated by a NULL caracter */
  212. /* two NULL character follows the end of the last variable.        */
  213.  
  214. /* This first loop scans PSP 'till it founds the PROMPT string      */
  215. /* or 'till reaches the end (\0\0)                                  */
  216.  
  217.   env_tmp = env;
  218.  
  219.   do
  220.   {
  221.      pr_pointer = strstr(env_tmp,"PROMPT=");
  222.      if (pr_pointer == NULL)
  223.      {
  224.           size -= strlen (env_tmp) + 1;
  225.           env_tmp = strrchr (env_tmp,'\0');
  226.           env_tmp++;
  227.      }
  228.   } while ( *env_tmp != '\0' && pr_pointer == NULL );
  229.  
  230.   if (pr_pointer != NULL)            /* if PROMPT present in PSP ...*/
  231.   {
  232.     pr_pointer += 7;
  233.     strcpy (&full[0],pr_pointer);        /* copy from the equal sign to \0 */
  234.     x = strstr(&full[0], &due[0]);
  235.     if (x != NULL)
  236.         size += (x - &full[0]) + 13 ;
  237.     size -= strlen (pr_pointer) + 8;
  238.     pr_pointer = strrchr(pr_pointer,'\0');      /* move to the end of the string */
  239.     pr_pointer++;                     /* move tho the next variable */
  240.     while ((*pr_pointer != '\0') || (*(pr_pointer - 1) != '\0'))
  241.     {
  242.         *env_tmp = *pr_pointer;
  243.         env_tmp++;
  244.         pr_pointer++;
  245.         size--;
  246.     }
  247.     *env_tmp = '\0';
  248.   }
  249.   else
  250.     full[0] = '\0';    /* if NO PROMPT found in PSP */
  251.   size--;
  252.   env = env_tmp;
  253. }
  254.  
  255. /* --------------------------------------------------------------------*/
  256. /*  This function writes the fortune inside the PROMPT variable        */
  257. /* --------------------------------------------------------------------*/
  258. write_in_psp()
  259. {
  260.   char far *env_tmp;
  261.   char *w;
  262.  
  263.   env_tmp = env++;
  264.  
  265.   strcat(env_tmp,"PROMPT=");
  266.                 /* put the first escape sequence */
  267.   strcat(env_tmp,&uno[0]);
  268.                 /* add the fortune */
  269.   strcat(env_tmp,&text[0]);
  270.                 /* now append the ending escape sequence */
  271.   strcat(env_tmp,&due[0]);
  272.                 /* There was already a fortune in PROMPT? ... */
  273.   w = strstr(&full[0],&due[0]);
  274.   if (w == NULL)
  275.   {
  276.                             /* ...NO!    */
  277.     strcat (env_tmp, &full[0]);
  278.     if (strlen (&full[0]) == 0)  strcat (env_tmp, "$p$g");
  279.   }
  280.   else
  281.                            /* ....Yes!    */
  282.   {
  283.     w = w + 13;
  284.     if (*w != '\0') strcat (env_tmp,w);
  285.   }
  286.   env_tmp = strrchr(env_tmp,'\0');            /* move to the end of the string */
  287.   *++env_tmp = '\0';
  288. }
  289.  
  290. /* --------------------------------------------------------------------*/
  291. /*    ...display the string                           */
  292. /* --------------------------------------------------------------------*/
  293. disp_string()
  294. {
  295.   printf ("%s\n",text);
  296. }
  297.  
  298. /* --------------------------------------------------------------------*/
  299. /*    reset PROMPT variable                */
  300. /* --------------------------------------------------------------------*/
  301. reset_pr()
  302. {
  303.     s